﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

namespace Kravl
{
    public class HttpWebRequest
    {
        public HttpWebRequest()
        {
        }

        /// <summary> TEST 1 </summary>
        public void GetWebpageUrls1(Uri httpUrl, ref System.Windows.Forms.TextBox t)
        {
            WebRequest request = WebRequest.Create(httpUrl);
            WebResponse response = null;
            try
            { 
                response= request.GetResponse();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "HttpWebRequest.GetWebpageUrls1(...)");
                return;
            }
            StreamReader r = new StreamReader(response.GetResponseStream());
            for (string line = r.ReadLine(); line != null; line = r.ReadLine())
            {
                //Console.WriteLine(line);
                // send output tilbage til sender
                t.Text += line + Environment.NewLine;
            }
        }


        /// <summary> TEST 3 </summary>
        public List<Uri> GetWebpageUrls2(Uri webPageUrl)
        {
            List<Uri> urls = new List<Uri>();

            WebRequest request = WebRequest.Create(webPageUrl);
            WebResponse response = null;
            try
            {
                response = request.GetResponse();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "HttpWebRequest.GetWebpageUrls2(...)");
                return urls;
            } 
            StreamReader r = new StreamReader(response.GetResponseStream());

            string pattern = "href=\"([-a-zA-Z_/#0-9]*).htm\">";
            MatchCollection matches = Regex.Matches(r.ReadToEnd(), pattern, RegexOptions.ExplicitCapture);
            foreach (Match m in matches)
            {
                string linkHostPath = webPageUrl.Scheme + "://" + webPageUrl.Host + webPageUrl.LocalPath;
                string linkPageName = m.Value.Replace("href=\"", "").Replace("\">", "");
                urls.Add(new Uri(linkHostPath + "/" + linkPageName));
            }

            return urls;
        }

        /// <summary> Find alle links fra websiden </summary>
        /// <param name="webPageUrl"> Note: path i linket må ikke have "." som 3. og 4. sidste tegn</param>
        public List<Uri> GetWebpageUrls3(Uri webPageUrl)
        {
            List<Uri> links = new List<Uri>();

            string startSquence = "<a href=\"";
            string endSequence = "\"";

            WebRequest request = WebRequest.Create(webPageUrl);
            WebResponse response = null;
            try
            {
                response = request.GetResponse();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "HttpWebRequest.GetWebpageUrls3(...)");
                return links;
            } 
            StreamReader r = new StreamReader(response.GetResponseStream());

            String rawCode = r.ReadToEnd();

            rawCode = rawCode.ToLower();

            while (rawCode.IndexOf("<a href") != -1)
            {
                int start = rawCode.IndexOf(startSquence) + startSquence.Length;
                int end = rawCode.IndexOf(endSequence, start);

                //Extract the link, and add it to the list
                if (end > start)
                {
                    string link = rawCode.Substring(start, end - start);

                    if (link != string.Empty)
                    {
                         // Tilføj path til html-dokumentet med en relative url.
                        if (!link.StartsWith("http://"))
                        {
                            string linkRoot = webPageUrl.Scheme + "://" + webPageUrl.Host;
                            for (int i = 0; i < webPageUrl.Segments.Length; i++ )
                            {
                                if (i == (webPageUrl.Segments.Length - 1)
                                    && (
                                    ( webPageUrl.Segments[i].Length >= 3 && webPageUrl.Segments[i].Substring(0, webPageUrl.Segments[i].Length-3).EndsWith(".") )


                                    || (webPageUrl.Segments[i].Length >= 4 && webPageUrl.Segments[i].Substring(0, webPageUrl.Segments[i].Length-4).EndsWith(".") ))
                                    )
                                {
                                    // tilføj ikke den sidste (som er htm-dokumentet)
                                }
                                else
                                {
                                    linkRoot += webPageUrl.Segments[i];
                                }
                            }

                            if(!linkRoot.EndsWith("/"))
                                linkRoot += "/";
                            linkRoot += link;
                            link = linkRoot;

                            ////It's a relative link, add a ..
                            //link = "../" + link;
                        }
                        else
                        {
                            // fuld link
                        }

                        
                        
                        Uri urilink = new Uri(link);
                        links.Add(urilink);
                    }
                }

                //Trim the raw data
                rawCode = rawCode.Substring(end + endSequence.Length);
            }

            return links;
        }









        #region test
        ///// <summary> Find alle links fra websiden </summary>
        ///// <param name="webPageUrl"> Note: path i linket må ikke have "." som 3. og 4. sidste tegn</param>
        //public List<TreeNode2> GetWebpageUrls4(string webpage, int maxDepth)
        //{
        //    Uri webp = new Uri(webpage);
            
        //    List<TreeNode2> links = new List<TreeNode2>();

        //    string startSquence = "<a href=\"";
        //    string endSequence = "\"";

        //    WebRequest request = WebRequest.Create(webp);
        //    WebResponse response = null;
        //    try
        //    {
        //        response = request.GetResponse();
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Windows.Forms.MessageBox.Show(ex.Message, "HttpWebRequest.GetWebpageUrls3(...)");
        //        return links;
        //    }
        //    StreamReader r = new StreamReader(response.GetResponseStream());

        //    String rawCode = r.ReadToEnd();

        //    rawCode = rawCode.ToLower();

        //    while (rawCode.IndexOf("<a href") != -1)
        //    {
        //        int start = rawCode.IndexOf(startSquence) + startSquence.Length;
        //        int end = rawCode.IndexOf(endSequence, start);

        //        //Extract the link, and add it to the list
        //        if (end > start)
        //        {
        //            string link = rawCode.Substring(start, end - start);

        //            if (link != string.Empty)
        //            {
        //                // Tilføj path til html-dokumentet med en relative url.
        //                if (!link.StartsWith("http://"))
        //                {
        //                    string linkRoot = webp.Scheme + "://" + webp.Host;
        //                    for (int i = 0; i < webp.Segments.Length; i++)
        //                    {
        //                        if (i == (webp.Segments.Length - 1)
        //                            && (
        //                            (webp.Segments[i].Length >= 3 && webp.Segments[i].Substring(0, webp.Segments[i].Length - 3).EndsWith("."))


        //                            || (webp.Segments[i].Length >= 4 && webp.Segments[i].Substring(0, webp.Segments[i].Length - 4).EndsWith(".")))
        //                            )
        //                        {
        //                            // tilføj ikke den sidste (som er htm-dokumentet)
        //                        }
        //                        else
        //                        {
        //                            linkRoot += webp.Segments[i];
        //                        }
        //                    }

        //                    if (!linkRoot.EndsWith("/"))
        //                        linkRoot += "/";
        //                    linkRoot += link;
        //                    link = linkRoot;

        //                    ////It's a relative link, add a ..
        //                    //link = "../" + link;
        //                }
        //                else
        //                {
        //                    // fuld link
        //                }


                      
        //                TreeNode2 urilink = new TreeNode2(link, maxDepth);
        //                links.Add(urilink);
        //            }
        //        }

        //        //Trim the raw data
        //        rawCode = rawCode.Substring(end + endSequence.Length);
        //    }

        //    links.ToArray();

        //    return links;
        //}
        #endregion//test

    }
}
